import axios from 'axios';
import common from '../common/js/common.js'

var Base64 = require('js-base64').Base64;
/*
请求报文格式一
{"traCode":"","data":{},"sysId":"0101","sign":"f51ea13e5ccc2cdd8f9c1c08b1958212","signType":"MD5","version":"1","reqSeq":"sn010116188348140064634102247375"}
请求报文格式二
{"des":"N","data":{"traCode":"","data":{},"sysId":"0101","sign":"f51ea13e5ccc2cdd8f9c1c08b1958212","signType":"MD5","version":"1","reqSeq":"sn010116188348140064634102247375"}}
*/
//登录
export const requestApiLogin = params => {
  return axios.post(`${common.base}/api`, params).then(res => {
    return res.data;
  });
};

//通用post
export const requestApiPost = params => {
  return axios.post(`${common.base}/api`, { "des": "N", "data": params }, {
    ContentType: "application/json;charset=UTF-8",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
    // return JSON.parse(data);
  });
};
/**导出订单 */
export const requestApiExportOrder = params => {
  let str = "";
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      const element = params[key];
      str = str + `${key}=${element}&`;
    }
  }
  return `${common.base}/downloadFile?${str}`;
}

/**导出员工列表 */
export const requestApiExportPerson = params => {
  let str = "";
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      const element = params[key];
      str = str + `${key}=${element}&`;
    }
  }
  return `${common.base}/imex/ExportEmployeeList?${str}`;
}

/**导出未审核列表 */
export const requestApiExportUnCheck = params => {
  let str = "";
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      const element = params[key];
      str = str + `${key}=${element}&`;
    }
  }
  return `${common.base}/imex/ExportUnauditedList?${str}`;
}
/**导出未审核员工列表 */
export const requestApiExportUnCheckPerson = params => {
  let str = "";
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      const element = params[key];
      str = str + `${key}=${element}&`;
    }
  }
  return `${common.base}/imex/ExportUnauditedPersonList?${str}`;
}


//图形验证码,文件流响应类型{responseType:"arraybuffer"}
export const requestApiCodePost = params => { return axios.post(`${common.base}/randImage`, { "des": "N", "data": params }, { responseType: "arraybuffer" }); }
//文件上传
export const requestApiFilePost = (file) => {
  axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
  let param = new FormData();
  param.append("file", file);
  return axios.post(`${common.base}/fileUpload`, param, {
    ContentType: "multipart/form-data;",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
  });
};

//批量上传
export const requestBatchUploadPost = (file) => {
  axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
  let param = new FormData();
  param.append("file", file);
  return axios.post(`${common.base}/importExcel`, param, {
    ContentType: "multipart/form-data;",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
  });
};

//批量新增模板
// export const requestImportAddPerson = (file,companyId,platformId) => {
//   axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
//   let param = new FormData();
//   param.append("file", file);
//   param.append("companyId", companyId);
//   param.append("platfromId", platformId);
//   return axios.post(`${common.base}/importAddPerson`, param, {
//     ContentType: "multipart/form-data;",
//     responseType: 'json',
//     timeout: 60000,
//     headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : ''  }
//   }).then(res => {
//     return res.data;
//   });
// };

/**导入员工 */
export const requestImportPerson = (file, companyId, platformId) => {
  axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
  let param = new FormData();
  param.append("file", file);
  param.append("companyId", companyId);
  param.append("platfromId", platformId);
  return axios.post(`${common.base}/imex/BatchAddPerson`, param, {
    ContentType: "multipart/form-data;",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
  });
}

/**导入审核列表 */
export const requestImportCheck = (file) => {
  axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
  let param = new FormData();
  param.append("file", file);
  return axios.post(`${common.base}/imex/ImportAuditList`, param, {
    ContentType: "multipart/form-data;",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
  });
};

/**导入停缴列表 */
export const requestImportStop = (file) => {
  axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;boundary = ' + new Date().getTime();
  let param = new FormData();
  param.append("file", file);
  return axios.post(`${common.base}/imex/BatchStopPayment`, param, {
    ContentType: "multipart/form-data;",
    responseType: 'json',
    timeout: 60000,
    headers: { "token": sessionStorage.getItem('login') ? JSON.parse(sessionStorage.getItem('login')).token : '' }
  }).then(res => {
    return res.data;
  });
};




export const requestRand = params => { return axios.get(`${common.base}/Image`, { params }); }

export const requestApiGet = params => {
  return axios.get(`${common.base}/api`, { params, responseType: 'json' });
};




export const downloadFile = (fileId, fileName) => {
  return axios.get(`${common.base}/downloadFile?fileId=` + fileId).then(res => {
    // if (window.navigator.msSaveBlob) {  // IE以及IE内核的浏览器
    //     try {
    //         window.navigator.msSaveBlob(res, fileName)  // res为接口返回数据，这里请求的时候已经处理了，如果没处理需要在此之前自行处理var data = new Blob([res.data]) 注意这里需要是数组形式的,fileName就是下载之后的文件名
    //         // window.navigator.msSaveOrOpenBlob(res, fileName);  //此方法类似上面的方法，区别可自行百度
    //     } catch (e) {
    //         console.log(e)
    //     }
    // } else {
    //     let url = window.URL.createObjectURL(new Blob([res]))
    //     let link = document.createElement('a')
    //     link.style.display = 'none'
    //     link.href = url
    //     link.setAttribute('download', fileName)// 文件名
    //     document.body.appendChild(link)
    //     link.click()
    //     document.body.removeChild(link) // 下载完成移除元素
    //     window.URL.revokeObjectURL(url) // 释放掉blob对象
    //     console.log(url)
    // }

  });
};




